<?php

/**
 * Material form base class.
 *
 * @package    alika
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseMaterialForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                => new sfWidgetFormInputHidden(),
      'photo_path'        => new sfWidgetFormInput(),
      'created_at'        => new sfWidgetFormDateTime(),
      'updated_at'        => new sfWidgetFormDateTime(),
      'stat'              => new sfWidgetFormInput(),
      'user_id'           => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => false)),
      'content'           => new sfWidgetFormTextarea(),
      'short_content'     => new sfWidgetFormTextarea(),
      'title'             => new sfWidgetFormTextarea(),
      'seo_keyword'       => new sfWidgetFormTextarea(),
      'seo_description'   => new sfWidgetFormTextarea(),
      'seo_title'         => new sfWidgetFormTextarea(),
      'link'              => new sfWidgetFormInput(),
      'see'               => new sfWidgetFormInput(),
      'click'             => new sfWidgetFormInput(),
      'city'              => new sfWidgetFormInput(),
      'country'           => new sfWidgetFormInput(),
      'custom_int1'       => new sfWidgetFormInput(),
      'custom_int2'       => new sfWidgetFormInput(),
      'custom_int3'       => new sfWidgetFormInput(),
      'custom_char1'      => new sfWidgetFormInput(),
      'custom_char2'      => new sfWidgetFormInput(),
      'custom_char3'      => new sfWidgetFormInput(),
      'custom_text1'      => new sfWidgetFormTextarea(),
      'custom_text2'      => new sfWidgetFormTextarea(),
      'custom_text3'      => new sfWidgetFormTextarea(),
      'custom_timestamp1' => new sfWidgetFormDateTime(),
      'custom_timestamp2' => new sfWidgetFormDateTime(),
      'custom_timestamp3' => new sfWidgetFormDateTime(),
      'custom_date1'      => new sfWidgetFormDate(),
      'custom_date2'      => new sfWidgetFormDate(),
      'custom_date3'      => new sfWidgetFormDate(),
      'custom_file1'      => new sfWidgetFormInput(),
      'custom_file2'      => new sfWidgetFormInput(),
      'custom_file3'      => new sfWidgetFormInput(),
      'custom_bool1'      => new sfWidgetFormInputCheckbox(),
      'custom_bool2'      => new sfWidgetFormInputCheckbox(),
      'custom_bool3'      => new sfWidgetFormInputCheckbox(),
      'type_id'           => new sfWidgetFormPropelChoice(array('model' => 'TypeMaterial', 'add_empty' => false)),
      'type_char'         => new sfWidgetFormInput(),
      'phone'             => new sfWidgetFormInput(),
      'adres'             => new sfWidgetFormInput(),
      'email'             => new sfWidgetFormInput(),
      'parent_id'         => new sfWidgetFormPropelChoice(array('model' => 'Material', 'add_empty' => true)),
      'child_id'          => new sfWidgetFormPropelChoice(array('model' => 'Folder', 'add_empty' => true)),
      'folder_id'         => new sfWidgetFormInput(),
      'region_id'         => new sfWidgetFormPropelChoice(array('model' => 'Region', 'add_empty' => true)),
      'pub'               => new sfWidgetFormInputCheckbox(),
      'contact_fio'       => new sfWidgetFormInput(),
      'contact_dolzhnost' => new sfWidgetFormInput(),
      'contact_email'     => new sfWidgetFormInput(),
      'contact_phone'     => new sfWidgetFormInput(),
      'fax'               => new sfWidgetFormInput(),
      'code_location'     => new sfWidgetFormTextarea(),
      'navigation_folder' => new sfWidgetFormInput(),
      'rating'            => new sfWidgetFormInput(),
      'cost'              => new sfWidgetFormInput(),
      'cant_delete'       => new sfWidgetFormInputCheckbox(),
      'folders_array'     => new sfWidgetFormInput(),
      'tag'               => new sfWidgetFormTextarea(),
      'is_video'          => new sfWidgetFormInputCheckbox(),
      'is_photo'          => new sfWidgetFormInputCheckbox(),
      'is_file'           => new sfWidgetFormInputCheckbox(),
      'is_news'           => new sfWidgetFormInputCheckbox(),
      'folder2tag_list'   => new sfWidgetFormPropelChoiceMany(array('model' => 'Folder')),
      'mat2_folder_list'  => new sfWidgetFormPropelChoiceMany(array('model' => 'Folder')),
      'mat2tag_list'      => new sfWidgetFormPropelChoiceMany(array('model' => 'Tag')),
    ));

    $this->setValidators(array(
      'id'                => new sfValidatorPropelChoice(array('model' => 'Material', 'column' => 'id', 'required' => false)),
      'photo_path'        => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'created_at'        => new sfValidatorDateTime(array('required' => false)),
      'updated_at'        => new sfValidatorDateTime(array('required' => false)),
      'stat'              => new sfValidatorInteger(array('required' => false)),
      'user_id'           => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id')),
      'content'           => new sfValidatorString(array('required' => false)),
      'short_content'     => new sfValidatorString(array('required' => false)),
      'title'             => new sfValidatorString(array('required' => false)),
      'seo_keyword'       => new sfValidatorString(array('required' => false)),
      'seo_description'   => new sfValidatorString(array('required' => false)),
      'seo_title'         => new sfValidatorString(array('required' => false)),
      'link'              => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'see'               => new sfValidatorInteger(array('required' => false)),
      'click'             => new sfValidatorInteger(array('required' => false)),
      'city'              => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'country'           => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_int1'       => new sfValidatorInteger(array('required' => false)),
      'custom_int2'       => new sfValidatorInteger(array('required' => false)),
      'custom_int3'       => new sfValidatorInteger(array('required' => false)),
      'custom_char1'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_char2'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_char3'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_text1'      => new sfValidatorString(array('required' => false)),
      'custom_text2'      => new sfValidatorString(array('required' => false)),
      'custom_text3'      => new sfValidatorString(array('required' => false)),
      'custom_timestamp1' => new sfValidatorDateTime(array('required' => false)),
      'custom_timestamp2' => new sfValidatorDateTime(array('required' => false)),
      'custom_timestamp3' => new sfValidatorDateTime(array('required' => false)),
      'custom_date1'      => new sfValidatorDate(array('required' => false)),
      'custom_date2'      => new sfValidatorDate(array('required' => false)),
      'custom_date3'      => new sfValidatorDate(array('required' => false)),
      'custom_file1'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_file2'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_file3'      => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'custom_bool1'      => new sfValidatorBoolean(array('required' => false)),
      'custom_bool2'      => new sfValidatorBoolean(array('required' => false)),
      'custom_bool3'      => new sfValidatorBoolean(array('required' => false)),
      'type_id'           => new sfValidatorPropelChoice(array('model' => 'TypeMaterial', 'column' => 'id')),
      'type_char'         => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'phone'             => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'adres'             => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'email'             => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'parent_id'         => new sfValidatorPropelChoice(array('model' => 'Material', 'column' => 'id', 'required' => false)),
      'child_id'          => new sfValidatorPropelChoice(array('model' => 'Folder', 'column' => 'id', 'required' => false)),
      'folder_id'         => new sfValidatorInteger(array('required' => false)),
      'region_id'         => new sfValidatorPropelChoice(array('model' => 'Region', 'column' => 'id', 'required' => false)),
      'pub'               => new sfValidatorBoolean(array('required' => false)),
      'contact_fio'       => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'contact_dolzhnost' => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'contact_email'     => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'contact_phone'     => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'fax'               => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'code_location'     => new sfValidatorString(array('required' => false)),
      'navigation_folder' => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'rating'            => new sfValidatorInteger(array('required' => false)),
      'cost'              => new sfValidatorNumber(array('required' => false)),
      'cant_delete'       => new sfValidatorBoolean(array('required' => false)),
      'folders_array'     => new sfValidatorString(array('max_length' => 250, 'required' => false)),
      'tag'               => new sfValidatorString(array('required' => false)),
      'is_video'          => new sfValidatorBoolean(array('required' => false)),
      'is_photo'          => new sfValidatorBoolean(array('required' => false)),
      'is_file'           => new sfValidatorBoolean(array('required' => false)),
      'is_news'           => new sfValidatorBoolean(array('required' => false)),
      'folder2tag_list'   => new sfValidatorPropelChoiceMany(array('model' => 'Folder', 'required' => false)),
      'mat2_folder_list'  => new sfValidatorPropelChoiceMany(array('model' => 'Folder', 'required' => false)),
      'mat2tag_list'      => new sfValidatorPropelChoiceMany(array('model' => 'Tag', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('material[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Material';
  }

  public function getI18nModelName()
  {
    return 'MaterialI18n';
  }

  public function getI18nFormClass()
  {
    return 'MaterialI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['folder2tag_list']))
    {
      $values = array();
      foreach ($this->object->getFolder2tags() as $obj)
      {
        $values[] = $obj->getFolderId();
      }

      $this->setDefault('folder2tag_list', $values);
    }

    if (isset($this->widgetSchema['mat2_folder_list']))
    {
      $values = array();
      foreach ($this->object->getMat2Folders() as $obj)
      {
        $values[] = $obj->getFolderId();
      }

      $this->setDefault('mat2_folder_list', $values);
    }

    if (isset($this->widgetSchema['mat2tag_list']))
    {
      $values = array();
      foreach ($this->object->getMat2tags() as $obj)
      {
        $values[] = $obj->getTagId();
      }

      $this->setDefault('mat2tag_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveFolder2tagList($con);
    $this->saveMat2FolderList($con);
    $this->saveMat2tagList($con);
  }

  public function saveFolder2tagList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['folder2tag_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Folder2tagPeer::MAT_ID, $this->object->getPrimaryKey());
    Folder2tagPeer::doDelete($c, $con);

    $values = $this->getValue('folder2tag_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Folder2tag();
        $obj->setMatId($this->object->getPrimaryKey());
        $obj->setFolderId($value);
        $obj->save();
      }
    }
  }

  public function saveMat2FolderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['mat2_folder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Mat2FolderPeer::MATERIAL_ID, $this->object->getPrimaryKey());
    Mat2FolderPeer::doDelete($c, $con);

    $values = $this->getValue('mat2_folder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Mat2Folder();
        $obj->setMaterialId($this->object->getPrimaryKey());
        $obj->setFolderId($value);
        $obj->save();
      }
    }
  }

  public function saveMat2tagList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['mat2tag_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(Mat2tagPeer::MAT_ID, $this->object->getPrimaryKey());
    Mat2tagPeer::doDelete($c, $con);

    $values = $this->getValue('mat2tag_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Mat2tag();
        $obj->setMatId($this->object->getPrimaryKey());
        $obj->setTagId($value);
        $obj->save();
      }
    }
  }

}
